#include <QtCore/QFile>
#include <QtXml/QDomDocument>
#include "qconstants.h"


QHash<QString, QString> QConstants::m_stringHash;


QConstants::QConstants()
{
    this->init();  // Q_ASSERT may cause error in release mode using vs2005 ?!
}

bool QConstants::init()
{
    if (m_stringHash.isEmpty()) {
        QFile file(":/strings.xml");
        if (!file.open(QFile::ReadOnly | QFile::Text)) {
            return false;
        }
        QDomDocument doc;
        if (!doc.setContent(file.readAll())) {
            return false;
        }
        QDomNodeList strList = doc.elementsByTagName("string");
        for (int i = 0; i < (int)strList.length(); i++) {
            QDomElement strElem = strList.at(i).toElement();
            QString strKey = strElem.attribute("key");
            QString strValue = strElem.text();
            m_stringHash.insert(strKey, strValue);
        }
    }

    return true;
}

QString QConstants::value(const QString &strKey)
{
    return m_stringHash.value(strKey);
}
